package importantDevice

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"xkginweb/commons/response"
	resp "xkginweb/model/entity/comms/response"
	"xkginweb/model/entity/importantDevice"
	"xkginweb/model/entity/importantDevice/request"
)

// 定义api接口
type ImportantDeviceApi struct{}

func (api *ImportantDeviceApi) CreateImportantDevice(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var importantDevice importantDevice.QccImportantDevice
	err := c.ShouldBindJSON(&importantDevice)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 创建实例，保存重要设备
	err = ImportantDeviceService.CreateImportantDevice(&importantDevice)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}

func (api *ImportantDeviceApi) UpdateImportantDevice(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var importantDevice importantDevice.QccImportantDevice
	err := c.ShouldBindJSON(&importantDevice)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = ImportantDeviceService.UpdateImportantDevice(&importantDevice)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("更新失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("更新成功", c)
}

// 根据ID查询重要设备明细

func (api *ImportantDeviceApi) GetImportantDevice(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)

	data, err := ImportantDeviceService.GetImportantDevice(uint(parseUint))
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

	response.Ok(data, c)
}

// http://localhost:8888/importantDevice/delete/:id

func (api *ImportantDeviceApi) DeleteById(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	err := ImportantDeviceService.DeleteImportantDeviceById(uint(parseUint))
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.Ok("ok", c)
}

func (api *ImportantDeviceApi) GetImportantDeviceDetail(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 这个是用来获取?age=123
	//age := c.Query("age")
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	data, err := ImportantDeviceService.GetImportantDevice(uint(parseUint))
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	response.Ok(data, c)
}

func (api *ImportantDeviceApi) LoadImportantDevicePage(c *gin.Context) {
	// 创建一个分页对象
	var pageInfo request.ImportantDevicePageInfo
	// 把前端json的参数传入给PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	importantDevicePage, total, err := ImportantDeviceService.LoadImportantDevicePage(pageInfo)
	if err != nil {
		response.FailWithMessage("获取失败"+err.Error(), c)
		return
	}
	response.Ok(resp.PageResult{
		List:     importantDevicePage,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, c)
}

func (api *ImportantDeviceApi) LoadImportantDevice(c *gin.Context) {
	var info request.ImportantDeviceInfo
	// 把前端json的参数传入给PageInfo
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	importantDeviceList, err := ImportantDeviceService.LoadImportantDevice(info)
	if err != nil {
		response.FailWithMessage("获取失败"+err.Error(), c)
		return
	}
	response.Ok(importantDeviceList, c)
}

// 添加带房间信息的重要设备

func (api *ImportantDeviceApi) CreateDeviceWithRoom(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var bindImportantDevice request.BindImportantDevice
	err := c.ShouldBindJSON(&bindImportantDevice)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	//room, err := RoomService.GetRoom(bindImportantDevice.RoomId)
	//if err != nil {
	//	response.FailWithMessage("房间不存在", c)
	//	return
	//}

	var context importantDevice.QccImportantDevice
	context.DeviceName = bindImportantDevice.DeviceName
	context.RoomId = bindImportantDevice.RoomId
	//context.RoomType = room.RoomType
	//context.RoomName = room.RoomName

	// 创建实例，保存重要设备
	err = ImportantDeviceService.CreateImportantDevice(&context)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}
